package app.Disthanitian.thaistyle.EmerMez;


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.ActionBar;
import android.app.Activity;
import android.app.ActionBar.OnNavigationListener;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.SpinnerAdapter;
import android.widget.TextView;
import android.widget.Toast;


public class EditMyAccount extends Activity {
	

	static UrlEmerMez URLlink = new UrlEmerMez();
	public static final String url = URLlink.getUrleditac();
	public static final String urleditname = URLlink.getUrleditname();
	
	public static String result = "";
	public static String result1 = "";
	InputStream is = null;
	String js_result = "";
	HttpPost httppost = null;
	EditText fname;
	EditText lname;
	String email;
	String id;
	//String userid;
	MemberUser user = new MemberUser();
	


	protected void onCreate(Bundle saveInstanceState) {
		super.onCreate(saveInstanceState);
		setContentView(R.layout.edit_myaccount);
		actionBar();
		
		WebPageTask task = new WebPageTask();
		task.execute();

	}
	
	public void actionBar(){
		
		ActionBar actionBar = getActionBar();
		actionBar.setDisplayHomeAsUpEnabled(true);
		actionBar.setTitle("Edit Profile");
		
	}

	public boolean onCreateOptionsMenu(Menu menu) {
		// TODO Auto-generated method stub
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.myname, menu);

		return true;
	}

	public boolean onOptionsItemSelected(MenuItem item) {
		// TODO Auto-generated method stub
		switch (item.getItemId()) {
		case R.id.action_ok:

			Runbg a = new Runbg();
			a.execute();
			break;
		case R.id.action_cancel:
			finish();
			
			break;
			
		}
		return super.onOptionsItemSelected(item);
	}

	public String EditName() {

		fname = (EditText) findViewById(R.id.editText_forgetpass);
		lname = (EditText) findViewById(R.id.editText_lastname);
		email =user.getEmail();
		
		String fname1 = fname.getText().toString().trim();
		String lname1 = lname.getText().toString().trim();
		
		List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
		nameValuePairs.add(new BasicNameValuePair("firstname",fname1));
		nameValuePairs.add(new BasicNameValuePair("lastname",lname1));
		nameValuePairs.add(new BasicNameValuePair("email",email));

		return EditData(urleditname,nameValuePairs);
	}
	
	private String EditData(String url,List<NameValuePair> nameValuePairs) {
		HttpClient httpclient = new DefaultHttpClient();
		HttpPost httppost = new HttpPost(urleditname);
		BufferedReader in = null;
		StringBuffer sb = new StringBuffer("");
		try {
			httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
			HttpResponse response = httpclient.execute(httppost);
			in = new BufferedReader(new InputStreamReader(response.getEntity()
					.getContent()));

			String line = "";
			String NL = System.getProperty("line.separator");
			while ((line = in.readLine()) != null) {
				sb.append(line + NL);
			}
			in.close();
			String page = sb.toString();
			return page;
		} catch (ClientProtocolException e) {
		} catch (IOException e) {
		}
		return null;
	}
	
	
	
	public String ShowName() {

		fname = (EditText) findViewById(R.id.editText_forgetpass);
		lname = (EditText) findViewById(R.id.editText_lastname);
		email =user.getEmail();

		List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
		nameValuePairs.add(new BasicNameValuePair("email",email));
		
		return postData(url,nameValuePairs);
	}

	public String postData(String url,List<NameValuePair> nameValuePairs) {

		HttpClient httpclient = new DefaultHttpClient();
		httppost = new HttpPost(url);
		BufferedReader in = null;
		StringBuffer sb = new StringBuffer("");

		try {
		    httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
			HttpResponse response = httpclient.execute(httppost);
			in = new BufferedReader(new InputStreamReader(response.getEntity()
					.getContent()));

			//StringBuilder sb = new StringBuilder();

			String line;
			String NL = System.getProperty("line.separator");
			while ((line = in.readLine()) != null) {
				sb.append(line + NL);
			}
			in.close();
			js_result = sb.toString();
			return js_result;
		} catch (ClientProtocolException e) {
			Log.e("log_tag", "Error converting result " + e.toString());
		} catch (IOException e) {
		}
		return null;
	}

	protected class WebPageTask extends AsyncTask<Void, Void, String> {

		@Override
		protected String doInBackground(Void... params) {

			result = ShowName();
			return result;
		}

		protected void onPostExecute(String result) {

			try {

				JSONArray jArray = new JSONArray(result);

				for (int i = 0; i < jArray.length(); i++) {
					JSONObject jo = jArray.getJSONObject(i);

					String firstname1 = jo.getString("firstname");
					String lastname1 = jo.getString("lastname");
					

					fname.setText(firstname1);
					lname.setText(lastname1);
					//user.setId(id);

				}
			} catch (JSONException e) {
				Log.e("log_tag", "Error parsing data " + e.toString());

			}

		}
	}
	
	protected class Runbg extends AsyncTask<Void, Void, String> {

		@Override
		protected String doInBackground(Void... params) {

			result1 = EditName();
			return result1;
		}

		protected void onPostExecute(String result1) {
			
			if (result1.trim().equals("ok")) {
				Toast.makeText(EditMyAccount.this,
						"Save Profile Success !!",
						Toast.LENGTH_LONG).show();
			} else if (result1.trim().equals("fail")) {
				Toast.makeText(EditMyAccount.this, "Save Profile Fail !!",
						Toast.LENGTH_LONG).show();
			} 
			
		}


	}
}
